window.ROOT = "";
(function() {
	// 增加按钮，弹窗清空
	jQuery('.addBtn').on('click', function() {
		
		jQuery('#id').val('');

		jQuery('#errorStatus').val('');
		
		jQuery('#errorStatusName').val('');
		
		jQuery("#ErrorStatusModal").modal('show');

	});

	var serverData = {};
	var serverTable;
	jQuery(document).ready(function() {
		serverTable = jQuery('#dataTable').DataTable({
			"serverSide" : true,
			"ordering" : false,
			"ajax" : {
				"dataType" : "json",
				"type" : "get",
				"cache" : false,
				"url" : window.rootPath
						+ '/ui/errorStatus/getpage',
				"data" : function(d) {
					return $.extend({}, d,
							serverData);
				}
			},
			columns : [
				{
					data : null,
					defaultContent : ""
				},
				{
					data : 'errorStatus'
				},
				{
					data : 'errorStatusName'
				},
				{
					data : null,
					defaultContent : "",
					render : function(data,
							type, full,
							meta) {
						var operate = '<button type="button" class="btn btn-sm btn-info editBtn" title="编辑"><i class="fa fa-pencil"></i></button>'
								+ '<button type="button" class="btn btn-sm btn-info delBtn" title="删除"><i class="fa fa-remove"></i></button>';

						return operate;

					}
				} ],
			"drawCallback" : function(oSettings) {
				var k = 0;
				var total = oSettings.aiDisplay.length
						+ oSettings._iDisplayStart;
	
				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
					$(
							'td:eq(0)',
							oSettings.aoData[oSettings.aiDisplay[k]].nTr)
							.text(i + 1);
					k++;
				}
	
				serverTable.off('click', 'tr');
				serverTable.on('click','tr',function(event) {
					event = event || window.event;
					var target = event.target || event.scrElement;
					var data = serverTable.row(this).data();

					if (target.className.indexOf('fa-remove') != -1|| target.className.indexOf('delBtn') != -1) {
						deleteModule(data);
					}

					if (target.className.indexOf('fa-pencil') != -1 || target.className.indexOf('editBtn') != -1) {
						editModule(data);
					}

					return;
				});
			}
		});
		var deleteModule = function(data) {
			if (!data)
				return;
			$.springLemonDialog({
				content : '确认删除：<' + data.errorStatus + '-' + data.errorStatusName + '>？',// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					// index:0 确认 1：取消
					// sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
					if (index == 0) {
						$("#right").mLoading("show");
						var url = window.rootPath + "/ui/errorStatus/delete/"+ data.id;
						jQuery.ajax({
							url : encodeURI(url),
							cache : false,
							type : "post",
							dataType : "json",
							success : function(data) {
								if(data.code == 0){
									serverTable.draw(false);
								}
								message(data.msg);
								$("#right").mLoading("hide");
							}
						});
					}
					$(this).closeDialog(modal);
				}

			});

		};

		var editModule = function(data) {
			if (!data) return;
			
			jQuery('#id').val(data.id);

			jQuery('#errorStatus').val(data.errorStatus);
			
			jQuery('#errorStatusName').val(data.errorStatusName);
			
			jQuery("#ErrorStatusModal").modal('show');
			
		};
		// 查询
		jQuery('.searchBtn').on('click', function() {
			serverData.key = jQuery("#search").val();
			serverTable.draw();
		});
		// 撤销查询
		jQuery('.resetBtn').on('click', function() {
			jQuery('#search').val('');
			serverData.key = '';
			serverTable.draw();
		});
		// 保存按钮
		jQuery('.saveBtn').on('click',function() {
			if (jQuery("#userEditForm") && !jQuery("#userEditForm").Validform({tiptype : 3,showAllError : true}).check(false)) {
				return;
			}
			
			var id= $('#id').val();
			var errorStatus= $('#errorStatus').val();
			var errorStatusName= $('#errorStatusName').val();
			
			var errorStatus = {
				"id" : id,
				"errorStatus" : errorStatus,
				"errorStatusName" : errorStatusName,
			}
			
			
			var url = window.rootPath + '/ui/errorStatus/save';
			$("#right").mLoading("show");
			jQuery.ajax({
				url : encodeURI(url),
				type : 'post',
				data : JSON.stringify(errorStatus),
				dataType : 'json',
				contentType : 'application/json;charset=utf-8',
				success : function(data) {
					message(data.msg);
					if(data.code == 0){
						jQuery('#ErrorStatusModal').modal('hide');
						serverTable.draw(false);
					}
					$("#right").mLoading("hide");
				}
			});
		});

	});

})();